package im.oen.boot.common.data;

import im.oen.boot.common.constant.Magic;
import im.oen.boot.common.utils.Checker;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author Kevin.Xu
 */
public class Page extends Base {
    /**
     * 排序
     */
    public static class Sort extends Base {
        /**
         * 顺序:true 倒序:false
         */
        private boolean asc;
        /**
         * 排序字段
         */
        private String filed;

        private Sort(String filed, boolean asc) {
            this.asc = asc;
            this.filed = filed;
        }

        public boolean isAsc() {
            return asc;
        }

        public String getFiled() {
            return filed;
        }

        public static Sort of(String filed, boolean asc) {
            return new Sort(filed, asc);
        }
    }

    /**
     * 分页数据的父类
     */
    public static class PageData extends Base {
        private Integer size = 10;
        private Integer page = 0;
        private LinkedHashSet<Sort> sorts;

        public Integer getSize() {
            if (Checker.isEmpty(size)) {
                return 10;
            }
            return size;
        }

        public void setSize(Integer size) {
            this.size = size;
        }

        public Integer getPage() {
            if (Checker.isEmpty(page)) {
                return 0;
            }
            return page;
        }

        public void setPage(Integer page) {
            this.page = page;
        }

        public LinkedHashSet<Sort> getSorts() {
            return sorts;
        }

        public void setSorts(LinkedHashSet<Sort> sorts) {
            this.sorts = sorts;
        }

        public void forEachSort(final Consumer<Sort> consumer) {
            if (Checker.isEmpty(sorts)) {
                return;
            }
            for (Sort sort : sorts) {
                if (Checker.isEmpty(sort.getFiled())) {
                    continue;
                }
                consumer.accept(sort);
            }
        }
    }

    /**
     * 分页数据的请求数据
     *
     * @param <T>
     */
    public static class Request<T extends Base> extends PageData {

        private T param;
        private Long start;
        private Long end;

        public T getParam() {
            return param;
        }

        public void setParam(T param) {
            this.param = param;
        }

        public Long getStart() {
            return start;
        }

        public void setStart(Long start) {
            this.start = start;
        }

        public Long getEnd() {
            return end;
        }

        public void setEnd(Long end) {
            this.end = end;
        }
    }

    /**
     * 分页数据的结果数据
     *
     * @param <T>
     */
    public static class Result<T extends Base> extends PageData {

        private Long total = Magic.L_ZERO;
        private List<T> contents;

        public Result() {
        }

        public static <T extends Base> Result<T> of(List<T> contents, long total, Request<T> request) {
            return Result.of(contents, total, request.getPage(), request.getSize(), request.getSorts());
        }

        public static <T extends Base> Result<T> of(List<T> contents, long total, int page, int size, LinkedHashSet<Sort> sorts) {
            Result<T> result = new Result();
            result.setPage(page);
            result.setSize(size);
            result.setTotal(total);
            result.setSorts(sorts);
            result.setContents(contents);
            return result;
        }

        public Long getTotal() {
            return total;
        }

        public void setTotal(Long total) {
            this.total = total;
        }

        public List<T> getContents() {
            return contents;
        }

        public void setContents(List<T> contents) {
            this.contents = contents;
        }
    }
}
